package com.nix.model.text;

import com.nix.model.page.NXTextPage;
import com.nix.model.page.area.NXAreaElement;
import com.nix.model.page.cursor.NXParagraphCursor;
import com.nix.model.page.cursor.NXTextWordCursor;
import com.nix.model.page.element.NXControl;
import com.nix.model.page.element.NXElement;
import com.nix.model.page.element.NXImage;
import com.nix.model.page.element.NXWord;
import com.nix.model.page.line.NXLine;
import com.nix.model.page.style.NXStyle;

public class NXTextView {
	
	
	private NXStyle myStyle;
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 处理页面
	 * @param page
	 * @param start
	 * @param result
	 */
	public void buildInfos(NXTextPage page, NXTextWordCursor start, NXTextWordCursor result){
		
		result.setCursor(start);
		int textAreaHeight = getTextAreaHeight();
		
		
		page.getLineInfos().clear();
		
		int counter = 0;
		
		do {
				
			NXParagraphCursor paragraphCursor = result.getMyParagraphCursor(); 
			
			final int wordIndex = result.getMyElementIndex();
			
			//TODO Control
			
			NXLine line = new NXLine(paragraphCursor, wordIndex, result.getMyCharIndex(),getStyle());
			final int endIndex = line.getParagraphLength();
			
			while(line.getEndElementIndex() != endIndex){
				
				
				line = fillTextLine(paragraphCursor, line.getEndElementIndex(), line.getEndStartCharIndex(), endIndex);
				
				textAreaHeight -= (line.getHeight() + line.getDescent());
				
				if(textAreaHeight < 0 && counter > 0){
					break;
				}
				
				//TODO
				
				result.moveTo(line.getEndElementIndex(), line.getEndStartCharIndex());
				page.getLineInfos().add(line);
				if (textAreaHeight < 0) {
					break;
				}
				counter++;
				
				//TODO
				
			}
			
			
		} while (result.isEndOfParagraph() && result.nextParagraph() && !result.getMyParagraphCursor().isEndOfSection() && (textAreaHeight >= 0));
		
		//TODO
		
	}
	
	
	
	/**
	 * 处理行
	 * @param paragraphCursor
	 * @param startIndex
	 * @param startCharIndex
	 * @param endIndex
	 * @return
	 */
	
	public NXLine fillTextLine(NXParagraphCursor paragraphCursor, 
			final int startIndex, final int startCharIndex, final int endIndex){
		
		//TODO
		NXLine line = new NXLine(paragraphCursor, startIndex, startCharIndex, getStyle());
		//TODO
		
		int currentElementIndex = startIndex;
		int currentCharIndex = startCharIndex;
		
		//TODO
		
		if (line.getEndElementIndex() == endIndex) {
			//TODO
			line.setEndElementIndex(endIndex);
			return line;
		}

		int newWidth = line.getWidth();
		int newHeight = line.getHeight();
		int newDescent = line.getDescent();
		
		
		//最大宽度
		int maxLength = getTextAreaWidth() - getStyle().getRightIndent();
		
		
		boolean isVisible = false;
		
		do {
			
			NXElement element = paragraphCursor.getElement(currentElementIndex);
			
			newWidth += getElementWidth(element);
			
			
			{
				int elementHeight = getElementHeight(element);
				if(elementHeight > newHeight){
					newHeight = elementHeight;
				}
			}
			
			{
				int elementDescent = getElementDescent();
				if(elementDescent > newDescent){
					newDescent = elementDescent;
				}
				
			}
			
			//TODO Deal Control And Other Situation
			
			
			if(newWidth > maxLength){
				break;
			}
			
			
			NXElement previousElement = element;
			++currentElementIndex;
			currentCharIndex = 0;
			boolean allowBreak = currentElementIndex == endIndex;
			if (!allowBreak) {
				element = paragraphCursor.getElement(currentElementIndex); 
				allowBreak = (((!(element instanceof NXWord)) || (previousElement instanceof NXWord)) && 
						!(element instanceof NXImage) && 
						!(element instanceof NXControl));
			}
			if (allowBreak) {
				
				line.setVisiable(isVisible);
				
				line.setWidth(newWidth);
				
				if(line.getHeight() < newHeight){
					line.setHeight(newHeight);
				}
				
				if(line.getDescent() < newDescent){
					line.setDescent(line.getDescent());
				}
				
				
				line.setEndElementIndex(currentElementIndex);
				line.setEndElementIndex(currentCharIndex);
				
				//TODO 样式等等的处理
				
			}
			
			
		} while (currentElementIndex < endIndex);
		
		
		//处理break 出来的情况 可能需要 回退 宽
		if ((currentElementIndex != endIndex) ){
			//TODO
		}
		
		
		return line;
		
	}
	
	
	/**
	 * 
	 * 行处理之 Area
	 * @param page
	 * @param line
	 * @param y 此处传入的y 为 元素 底部位置
	 */
	public void fillTextAreaLine(NXTextPage page,NXLine line,int y){
		
		//TODO
		int x = 0;//需要处理 开始
		
		NXParagraphCursor paragraph = line.getParagraphCursor();
		int paragraphIndex = paragraph.Index;//段之概念
		
		int endElementIndex = line.getEndElementIndex();
		int startCharIndex = line.getRealStartCharIndex();
		int charIndex = line.getRealStartCharIndex();
		NXAreaElement spaceElement = null;
		
		for(int wordIndex = startCharIndex ; wordIndex < endElementIndex ; wordIndex++){
			
			final NXElement element = paragraph.getElement(wordIndex);

			if(element != null){
				
				//TODO 处理宽 等
				
				if(element instanceof NXWord){
					
					int length = element.Length;
					int width = getElementWidth(element);
					int height = getElementHeight(element);
					int descent = getElementDescent();
					spaceElement = new NXAreaElement(paragraphIndex,wordIndex,charIndex,length - charIndex,getStyle(), element, 
							x, x + width - 1, y - height + 1, y + descent);
					page.getElementAreaVector().add(spaceElement);
					
					
				}
				else if(element instanceof NXImage){
					//TODO
				}
				else{
					//TODO
				}
				
			}
		}
		
		
		
	}
	
	
	
	/**
	 * 展示页面宽
	 * @return
	 */
	public int getTextAreaWidth(){
		//TODO
		return 0;
	}
	
	public int getTextAreaHeight(){
		//TODO
		return 0;
	}
	
	
	public int getElementDescent(){
		//TODO
		return 0;
	}
	
	public int getElementHeight(NXElement element){
		//TODO
		return 1;
	}
	
	public int getElementWidth(NXElement element){
		//TODO
		return 0;
	}
	
	public NXStyle getStyle(){
		return myStyle;
	}
	
	
}
